package com.ln.lnzw.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.ln.lnzw.MainActivity;
import com.ln.lnzw.R;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.RegistBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.Base64Util;
import com.ln.lnzw.utils.BitmapUtil;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.utils.TokenUtils;
import com.ln.lnzw.utils.WorkDetailsUtils;
import com.ln.lnzw.view.ClearableEditText;
import com.ln.lnzw.view.CommonPopupWindow;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import static com.ln.lnzw.activity.PersonalInformationSettingsActivity.getImageContentUri;
import static com.ln.lnzw.activity.PersonalInformationSettingsActivity.getRealFilePath;

/**
 * Created by wuwei on 2018/5/25.
 * 法人注册
 */

public class LegalPersonRegisteredActivity extends BaseActivity implements CommonPopupWindow.ViewInterface {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_yanzheng)
    ImageView ivYanzheng;
    @BindView(R.id.tv_yanzheng)
    TextView tvYanzheng;
    @BindView(R.id.yanzheng)
    LinearLayout yanzheng;
    @BindView(R.id.iv_yonghu)
    ImageView ivYonghu;
    @BindView(R.id.tv_yonghu)
    TextView tvYonghu;
    @BindView(R.id.ll_yonghu)
    LinearLayout llYonghu;
    @BindView(R.id.iv_set)
    ImageView ivSet;
    @BindView(R.id.tv_set)
    TextView tvSet;
    @BindView(R.id.ll_set)
    LinearLayout llSet;
    @BindView(R.id.rl_tijaio1)
    RelativeLayout rlTijaio1;
    @BindView(R.id.rl_verifyphone)
    RelativeLayout rlVerifyphone;
    @BindView(R.id.rl_tijaio2)
    RelativeLayout rlTijaio2;
    @BindView(R.id.rl_tijaio3)
    RelativeLayout rlTijaio3;
    @BindView(R.id.rl_userinfo)
    LinearLayout rlUserinfo;
    @BindView(R.id.rl_registered)
    RelativeLayout rlRegistered;
    @BindView(R.id.ll_setmima)
    LinearLayout llSetmima;
    @BindView(R.id.tv_tongyi)
    TextView tvTongyi;
    @BindView(R.id.rl_tongyi)
    RelativeLayout rlTongyi;
    @BindView(R.id.phone)
    ClearableEditText phone;
    @BindView(R.id.cet_yanzhengma)
    ClearableEditText cetYanzhengma;
    @BindView(R.id.cet_name)
    ClearableEditText cetName;
    @BindView(R.id.cet_truename)
    ClearableEditText cetTruename;
    @BindView(R.id.cet_shenfenzheng)
    ClearableEditText cetShenfenzheng;
    @BindView(R.id.cet_mima_one)
    ClearableEditText cetMimaOne;
    @BindView(R.id.cet_mima_two)
    ClearableEditText cetMimaTwo;
    @BindView(R.id.cet_youxiang)
    ClearableEditText cetYouxiang;
    @BindView(R.id.iv_duihao)
    ImageView ivDuihao;
    @BindView(R.id.iv_qiye)
    ImageView ivQiye;
    @BindView(R.id.tv_qiye)
    TextView tvQiye;
    @BindView(R.id.ll_qiyeinfo)
    LinearLayout llQiyeinfo;
    @BindView(R.id.cet_qiyequancheng)
    ClearableEditText cetQiyequancheng;
    @BindView(R.id.cet_xinyongdaima)
    ClearableEditText cetXinyongdaima;
    @BindView(R.id.cet_dengjijiguan)
    ClearableEditText cetDengjijiguan;
    @BindView(R.id.cet_daibiaoren)
    ClearableEditText cetDaibiaoren;
    @BindView(R.id.cet_farenzhengjianhao)
    ClearableEditText cetFarenzhengjianhao;
    @BindView(R.id.iv_shangchuan)
    ImageView ivShangchuan;
    @BindView(R.id.tv_enterpriseType)
    TextView tvEnterpriseType;
    @BindView(R.id.rl_enterpriseType)
    RelativeLayout rlEnterpriseType;
    @BindView(R.id.tv_legalDocumenType)
    TextView tvLegalDocumenType;
    @BindView(R.id.rl_legalDocumenType)
    RelativeLayout rlLegalDocumenType;
    private int flag = 0;
    private boolean tag = true;
    CommonPopupWindow popupWindow;
    public static final int Cut_PHOTO = 1;
    public static final int SHOW_PHOTO = 2;
    public static final int PHOTO_ALBUM = 3;
    public static final int SHOW_PHOTO_ALBUM = 4;
    public Uri imageUriFromCamera;
    public final String USER_IMAGE_NAME = "image.png";
    File outputImage;
    File photoFile;
    private Uri imageUri;
    private Uri uri;
    private Bitmap bitmap;
    private List<String> enterpriseType = new ArrayList<>();//企业类型
    private List<String> legalDocumenType = new ArrayList<>();//法人证件类型
    private OptionsPickerView  pvNoLinkOptionsEnterprise;
    private OptionsPickerView  pvNoLinkOptionsLegalDocumen;
    private String enterpriseTypeCode;//企业类型对应的值
    private String legalDocumenTypeCode;//法人证件类型对应值
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_legal_person_registered);
        ButterKnife.bind(this);
        getOptionData();
        setListener();
    }

    private void getOptionData() {
        enterpriseType.add("盈利性企业");
        enterpriseType.add("事业单位");
        enterpriseType.add("社会团体");

        legalDocumenType.add("身份证");
        legalDocumenType.add("护照");
        legalDocumenType.add("台湾居民来往大陆通行证");
        legalDocumenType.add("港澳居民来往内地通行证");
        initNoLinkOptionsPickerEnterpriseType();
        initNoLinkOptionsPickerLegalDocumenType();

//
    }

    private void initNoLinkOptionsPickerLegalDocumenType() {
        pvNoLinkOptionsLegalDocumen = new OptionsPickerBuilder(activity, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                //返回的分别是三个级别的选中位置
                String tx = legalDocumenType.get(options1);
                tvLegalDocumenType.setText(tx);
                legalDocumenTypeCode= WorkDetailsUtils.getLegalDocumenTypeCode(tx);
            }
        }).build();
        pvNoLinkOptionsLegalDocumen.setPicker(legalDocumenType);
    }

    private void initNoLinkOptionsPickerEnterpriseType() {
        pvNoLinkOptionsEnterprise = new OptionsPickerBuilder(activity, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                //返回的分别是三个级别的选中位置
                String tx = enterpriseType.get(options1);
                tvEnterpriseType.setText(tx);
                enterpriseTypeCode=WorkDetailsUtils.getEnterpriseTypeCode(tx);
            }
        }).build();
        pvNoLinkOptionsEnterprise.setPicker(enterpriseType);
    }


    private void setListener() {
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });

        cetYanzhengma.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
        cetName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetName.getText()) && !TextUtils.isEmpty(cetTruename.getText()) && !TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }

            }
        });
        cetTruename.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetName.getText()) && !TextUtils.isEmpty(cetTruename.getText()) && !TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetShenfenzheng.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetName.getText()) && !TextUtils.isEmpty(cetTruename.getText()) && !TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    rlTijaio2.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetQiyequancheng.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetQiyequancheng.getText()) || TextUtils.isEmpty(cetXinyongdaima.getText()) || TextUtils.isEmpty(cetDengjijiguan.getText()) || TextUtils.isEmpty(cetDaibiaoren.getText()) || TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetQiyequancheng.getText()) && !TextUtils.isEmpty(cetXinyongdaima.getText()) && !TextUtils.isEmpty(cetDengjijiguan.getText()) && !TextUtils.isEmpty(cetDaibiaoren.getText()) && !TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetXinyongdaima.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetQiyequancheng.getText()) || TextUtils.isEmpty(cetXinyongdaima.getText()) || TextUtils.isEmpty(cetDengjijiguan.getText()) || TextUtils.isEmpty(cetDaibiaoren.getText()) || TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetQiyequancheng.getText()) && !TextUtils.isEmpty(cetXinyongdaima.getText()) && !TextUtils.isEmpty(cetDengjijiguan.getText()) && !TextUtils.isEmpty(cetDaibiaoren.getText()) && !TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetDengjijiguan.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetQiyequancheng.getText()) || TextUtils.isEmpty(cetXinyongdaima.getText()) || TextUtils.isEmpty(cetDengjijiguan.getText()) || TextUtils.isEmpty(cetDaibiaoren.getText()) || TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetQiyequancheng.getText()) && !TextUtils.isEmpty(cetXinyongdaima.getText()) && !TextUtils.isEmpty(cetDengjijiguan.getText()) && !TextUtils.isEmpty(cetDaibiaoren.getText()) && !TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetDaibiaoren.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetQiyequancheng.getText()) || TextUtils.isEmpty(cetXinyongdaima.getText()) || TextUtils.isEmpty(cetDengjijiguan.getText()) || TextUtils.isEmpty(cetDaibiaoren.getText()) || TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetQiyequancheng.getText()) && !TextUtils.isEmpty(cetXinyongdaima.getText()) && !TextUtils.isEmpty(cetDengjijiguan.getText()) && !TextUtils.isEmpty(cetDaibiaoren.getText()) && !TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });
        cetFarenzhengjianhao.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(cetQiyequancheng.getText()) || TextUtils.isEmpty(cetXinyongdaima.getText()) || TextUtils.isEmpty(cetDengjijiguan.getText()) || TextUtils.isEmpty(cetDaibiaoren.getText()) || TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                }
                if (!TextUtils.isEmpty(cetQiyequancheng.getText()) && !TextUtils.isEmpty(cetXinyongdaima.getText()) && !TextUtils.isEmpty(cetDengjijiguan.getText()) && !TextUtils.isEmpty(cetDaibiaoren.getText()) && !TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    rlTijaio3.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                }
            }
        });

        cetMimaOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (tag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });
        cetMimaTwo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (tag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });
        cetYouxiang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (tag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });

    }

    private void editTextIsNo() {
        if (TextUtils.isEmpty(phone.getText()) || TextUtils.isEmpty(cetYanzhengma.getText())) {
            rlTijaio1.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    private void editTextIsYes() {
        if (!TextUtils.isEmpty(phone.getText()) && !TextUtils.isEmpty(cetYanzhengma.getText())) {
            rlTijaio1.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }

    @OnClick({R.id.iv_back, R.id.yanzheng, R.id.tv_tongyi, R.id.rl_tongyi, R.id.ll_yonghu, R.id.ll_set, R.id.rl_tijaio1, R.id.rl_tijaio2, R.id.rl_tijaio3, R.id.rl_registered, R.id.ll_setmima, R.id.iv_shangchuan, R.id.rl_enterpriseType,R.id.rl_legalDocumenType})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                Log.i("123", "onViewClicked: " + flag);
                if (flag == 0) {
                    finish();
                } else if (flag == 1) {  //1-0
                    flag = 0;
                    rlVerifyphone.setVisibility(View.VISIBLE);
                    rlUserinfo.setVisibility(View.GONE);
                    //恢复
                    ivYonghu.setImageResource(R.mipmap.icon_yhxx_no);
                    tvYonghu.setTextColor(getResources().getColor(R.color.mine_small_color));
                } else if (flag == 2) { //2-1
                    flag = 1;
                    llQiyeinfo.setVisibility(View.GONE);
                    rlUserinfo.setVisibility(View.VISIBLE);
                    ivQiye.setImageResource(R.mipmap.icon_qyxx_no);
                    tvQiye.setTextColor(getResources().getColor(R.color.mine_small_color));
                } else {
                    flag = 2;
                    llSetmima.setVisibility(View.GONE);
                    llQiyeinfo.setVisibility(View.VISIBLE);
                    ivSet.setImageResource(R.mipmap.icon_szmm_no);
                    tvSet.setTextColor(getResources().getColor(R.color.mine_small_color));
                }

                break;
            case R.id.rl_tijaio1:
                if (TextUtils.isEmpty(phone.getText()) || TextUtils.isEmpty(cetYanzhengma.getText())) {
                    return;
                }
                if (!TextUtils.isEmpty(phone.getText()) && !TextUtils.isEmpty(cetYanzhengma.getText())) {
//                    if (!RegexUtils.isMobileExact(cetPhone.getText())){
//                        ToastFactory.getToast(activity, "请输入正确的手机号").show();
//                        return;
//                    }
                }
                flag = 1;
                rlVerifyphone.setVisibility(View.GONE);
                rlUserinfo.setVisibility(View.VISIBLE);
                ivYonghu.setImageResource(R.mipmap.icon_yhxx_yes);
                tvYonghu.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;
            case R.id.rl_tijaio2:
                if (TextUtils.isEmpty(cetName.getText()) || TextUtils.isEmpty(cetTruename.getText()) || TextUtils.isEmpty(cetShenfenzheng.getText())) {
                    return;
                }
                flag = 2;
                llQiyeinfo.setVisibility(View.VISIBLE);
                rlUserinfo.setVisibility(View.GONE);
                ivQiye.setImageResource(R.mipmap.icon_qyxx_yes);
                tvQiye.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;

            case R.id.rl_tijaio3:
                if (TextUtils.isEmpty(cetQiyequancheng.getText()) || TextUtils.isEmpty(cetXinyongdaima.getText()) || TextUtils.isEmpty(cetDengjijiguan.getText()) || TextUtils.isEmpty(cetDaibiaoren.getText()) || TextUtils.isEmpty(cetFarenzhengjianhao.getText())) {
                    return;
                }
                flag = 3;
                llSetmima.setVisibility(View.VISIBLE);
                llQiyeinfo.setVisibility(View.GONE);
                ivSet.setImageResource(R.mipmap.icon_szmm_yes);
                tvSet.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                break;

            case R.id.rl_registered:
                if (tag) {

                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        //判断俩次密码是否一致
                        if (!cetMimaOne.getText().toString().equals(cetMimaTwo.getText().toString())) {
                            ToastFactory.getToast(this, "密码不一致").show();
                            return;
                        }
                        //判断邮箱格式
                        if (!RegexUtils.isEmail(cetYouxiang.getText())) {
                            ToastFactory.getToast(this, "邮箱格式错误").show();
                            return;
                        }
//                        ActivityUtils.startActivity(activity, VerifiedActivity.class);
                        initData();//注册请求
                    }
                }

                break;
            case R.id.tv_tongyi:
                Bundle b = new Bundle();
                b.putString("webUrl", "file:///android_asset/LNnet.html");
                b.putString("name", "辽宁政府服务APP用户协议");
                ActivityUtils.startActivity(b, activity, UserAgreementActivity.class);
                break;
            case R.id.rl_tongyi:
                if (tag) {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_yes);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_yes);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    tag = false;
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_no);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_no);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    tag = true;
                }
                break;

            case R.id.iv_shangchuan:
                showPopupWindow();
                break;
            case R.id.rl_enterpriseType://企业
                pvNoLinkOptionsEnterprise.show();
                break;
            case R.id.rl_legalDocumenType:
                pvNoLinkOptionsLegalDocumen.show();
                break;
        }
    }

    private void initData() {
        Log.i("555", "initData: "+TokenUtils.getInstance().GetGUID());
        Log.i("555", "initData: "+BitmapUtil.convertIconToString(bitmap));
        Log.i("555", "initData: "+enterpriseTypeCode);
        Log.i("555", "initData: "+cetQiyequancheng.getText().toString());
        Log.i("555", "initData: "+cetXinyongdaima.getText().toString());
        Log.i("555", "initData: "+cetDengjijiguan.getText().toString());
        Log.i("555", "initData: "+cetDaibiaoren.getText().toString());
        Log.i("555", "initData: "+legalDocumenTypeCode);
        Log.i("555", "initData: "+cetFarenzhengjianhao.getText().toString());
        Log.i("555", "initData: "+cetName.getText().toString());
        Log.i("555", "initData: "+cetMimaOne.getText().toString());
        Log.i("555", "initData: "+cetTruename.getText().toString());
        Log.i("555", "initData: "+cetShenfenzheng.getText().toString());
        Log.i("555", "initData: "+cetYouxiang.getText().toString());
        Log.i("555", "initData: "+phone.getText().toString());
        Log.i("555", "initData: "+cetYanzhengma.getText().toString());
        //构建body
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("file", BitmapUtil.convertIconToString(compressImage(bitmap)))
                .addFormDataPart("enterpriseType", enterpriseTypeCode)
                .addFormDataPart("enterpriseName", cetQiyequancheng.getText().toString())
                .addFormDataPart("businessLicense", cetXinyongdaima.getText().toString())
                .addFormDataPart("registeredAuthority", cetDengjijiguan.getText().toString())
                .addFormDataPart("legalPerson", cetDaibiaoren.getText().toString())
                .addFormDataPart("legalPersonCertificateType", legalDocumenTypeCode)
                .addFormDataPart("legalPersonID", cetFarenzhengjianhao.getText().toString())
                .addFormDataPart("loginAccountName", cetName.getText().toString())
                .addFormDataPart("personNewPassword", cetMimaOne.getText().toString())
                .addFormDataPart("trueName", cetTruename.getText().toString())
                .addFormDataPart("personCreditID", cetShenfenzheng.getText().toString())
                .addFormDataPart("email", cetYouxiang.getText().toString())
                .addFormDataPart("mobile", phone.getText().toString())
                .addFormDataPart("mobileCheckCode", cetYanzhengma.getText().toString())
                .build();
        HttpMethods.getInstanceCenter().info.legalPersonRegist(requestBody/*TokenUtils.getInstance().GetGUID(), BitmapUtil.convertIconToString(compressImage(bitmap)),enterpriseTypeCode,cetQiyequancheng.getText().toString(),cetXinyongdaima.getText().toString(),cetDengjijiguan.getText().toString(),cetDaibiaoren.getText().toString(),legalDocumenTypeCode,cetFarenzhengjianhao.getText().toString(),cetName.getText().toString(),cetMimaOne.getText().toString(),cetTruename.getText().toString(),cetShenfenzheng.getText().toString(),cetYouxiang.getText().toString(),phone.getText().toString(),cetYanzhengma.getText().toString()*/)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RegistBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RegistBean registBean) {
                        Log.i("555", "onNext: "+registBean.toString());
                        if ("200".equals(registBean.getCode())) {
                            ToastFactory.getToast(activity,registBean.getMsg()).show();
//                            ActivityUtils.startActivity(activity,VerifiedActivity.class);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("555", "onNext: "+e.toString());
                    }

                    @Override
                    public void onComplete() {
                        finish();
                    }
                });

    }

    public void showPopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) return;
        View upView = LayoutInflater.from(this).inflate(R.layout.popup_up, null);
        //测量View的宽高
        CommonPopupWindow.measureWidthAndHeight(upView);
        popupWindow = new CommonPopupWindow.Builder(this)
                .setView(R.layout.popup_up)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                .setAnimationStyle(R.style.AnimUp)
                .setViewOnclickListener(this)
                .create();

        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onBackPressedSupport() {
        if (flag == 0) {
            super.onBackPressedSupport();
        } else if (flag == 1) {  //1-0
            flag = 0;
            rlVerifyphone.setVisibility(View.VISIBLE);
            rlUserinfo.setVisibility(View.GONE);
            //恢复
            ivYonghu.setImageResource(R.mipmap.icon_yhxx_no);
            tvYonghu.setTextColor(getResources().getColor(R.color.mine_small_color));
        } else if (flag == 2) { //2-1
            flag = 1;
            llQiyeinfo.setVisibility(View.GONE);
            rlUserinfo.setVisibility(View.VISIBLE);
            ivQiye.setImageResource(R.mipmap.icon_szmm_no);
            tvQiye.setTextColor(getResources().getColor(R.color.mine_small_color));
        } else {
            flag = 2;
            llSetmima.setVisibility(View.GONE);
            llQiyeinfo.setVisibility(View.VISIBLE);
            ivSet.setImageResource(R.mipmap.icon_szmm_no);
            tvSet.setTextColor(getResources().getColor(R.color.mine_small_color));
        }

    }

    public void phone1() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        photoFile = createImagePathFile(this);
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

                                /*
                                * 这里就是高版本需要注意的，需用使用FileProvider来获取Uri，同时需要注意getUriForFile
                                * 方法第二个参数要与AndroidManifest.xml中provider的里面的属性authorities的值一致
                                * */
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        imageUriFromCamera = FileProvider.getUriForFile(this,
                "com.ln.lnzw.fileprovider", photoFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUriFromCamera);

        startActivityForResult(intent, Cut_PHOTO); //
    }

    public void album() {
        outputImage = new File(Environment.getExternalStorageDirectory(),
                "user_head" + System.currentTimeMillis() + ".jpg");
        try {
            if (outputImage.exists()) {
                outputImage.delete();
            }
            outputImage.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageUri = FileProvider.getUriForFile(this,
                "com.ln.lnzw.fileprovider", outputImage); //getApplicationContext().getPackageName() + ".provider"
//        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.setType("image/*");
        //允许裁剪
        intent.putExtra("crop", true);
        //允许缩放
        intent.putExtra("scale", true);
        //图片的输出位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, PHOTO_ALBUM);
    }

    public File createImagePathFile(Activity activity) {
        //文件目录可以根据自己的需要自行定义
        Uri imageFilePath;
        File file = new File(activity.getExternalCacheDir(), USER_IMAGE_NAME);
        imageFilePath = Uri.fromFile(file);
        return file;
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.popup_up:
                TextView camera = (TextView) view.findViewById(R.id.tv_camera);
                TextView phone = (TextView) view.findViewById(R.id.tv_phone);
                Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        phone1();
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                phone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        album();
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                        return true;
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case Cut_PHOTO:
                if (resultCode == RESULT_OK) {
                    if (bitmap != null && !bitmap.isRecycled()) {
                        // 回收并且置为null
                        bitmap.recycle();
                        bitmap = null;
                    }
                    System.gc();
                    try {
                        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUriFromCamera));

                        // 将裁剪后的照片显示出来
                        ivShangchuan.setImageBitmap(ImageUtils.scale(bitmap, ivShangchuan.getWidth(), ivShangchuan.getHeight()));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case PHOTO_ALBUM:
                Log.i("123", "onActivityResult: " + PHOTO_ALBUM);
                if (resultCode == RESULT_OK) {
                    uri = data.getData();
                    if (uri.toString().contains("com.miui.gallery.open")) {
                        uri = getImageContentUri(activity, new File(getRealFilePath(activity, uri)));
                    }
                    if (bitmap != null && !bitmap.isRecycled()) {
                        // 回收并且置为null
                        bitmap.recycle();
                        bitmap = null;
                    }
                    System.gc();
                    try {
                        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                        ivShangchuan.setImageBitmap(ImageUtils.scale(bitmap, ivShangchuan.getWidth(), ivShangchuan.getHeight()));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }

                break;
            default:
                break;
        }
    }
    public static Bitmap compressImage(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 90;

        while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset(); // 重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%，把压缩后的数据存放到baos中
            options -= 10;// 每次都减少10
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
        return bitmap;
    }
}
